﻿using System.Collections.Generic;

namespace Sidvall.Reflection
{
    public class InstanceItemCollection : System.Collections.ObjectModel.Collection<InstanceItem>
    {
        #region Public Members

        #region Add

        public void Add(string factoryTypeName, string context)
        {
            var item = new InstanceItem()
            {
                FactoryTypeName = factoryTypeName,
                Context = context,
            };
            Add(item);
        }

        #endregion
        #region AddRange

        public void AddRange(IEnumerable<InstanceItem> items)
        {
            if (items == null)
                return;
            foreach (var item in items)
                Add(item);
        }

        #endregion

        #region Deserialize

        public static InstanceItemCollection Deserialize(string value)
        {
            return Deserialize(value, null);
        }
        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design", "CA1031:DoNotCatchGeneralExceptionTypes")]
        public static InstanceItemCollection Deserialize(string value, InstanceItemCollection defaultValue)
        {
            return Sidvall.Serialization.SerializerManager.Current.TryDeserialize<InstanceItemCollection>(value, () =>
            {
                return defaultValue;
            });
        }

        #endregion
        #region Serialize

        public string Serialize()
        {
            return Serialize(this);
        }
        public static string Serialize(InstanceItemCollection context)
        {
            return Sidvall.Serialization.SerializerManager.Current.SerializeObject(context);
        }

        #endregion

        #endregion
    }
}
